package com.su.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Service;

import com.su.tools.DaoSupport1;
import com.su.tools.PageData;

import net.sf.json.JSONObject;

@Service("itemCategoryService")
public class ItemCategoryService {

	@Resource(name = "daoSupport1")
	private DaoSupport1 dao;
	private JSONObject json = new JSONObject();
	private PageData retPd;
	private List<PageData> retListPd;

	public JSONObject invoke(PageData pd) throws Exception {

		if (pd.getString("params").equals("IC101")) {
			this.IC101(pd);
		} else if (pd.getString("params").equals("IC102")) {
			this.IC102(pd);
		} else if (pd.getString("params").equals("IC103")) {
			this.IC103(pd);
		} else if (pd.getString("params").equals("IC104")) {
			this.IC104(pd);
		} else if (pd.getString("params").equals("IC105")) {
			this.IC105(pd);
		} else if (pd.getString("params").equals("IC106")) {
			this.IC106(pd);
		}  else {
			json.put("code", "100");
			json.put("msg", "没有找到该业务代码");
			return json;
		}
		return json;
	}

	/**
	 * 查询项目大类及项目分类
	 * 
	 * @param pd
	 * @throws Exception
	 */
	private void IC101(PageData pd) throws Exception {
		retListPd = dao.getList("ItemCategoryMapper.getTreeCategory", pd);
		if (retListPd.size() == 0) {
			json.put("result", "");
		} else {
			json.put("result", retListPd);
		}
	}

	/**
	 * 查询项目大类
	 * 
	 * @param pd
	 * @throws Exception
	 */
	private void IC102(PageData pd) throws Exception {
		retListPd = dao.getList("ItemCategoryMapper.getDCategory", pd);
		if (retListPd.size() == 0) {
			json.put("result", "");
		} else {
			json.put("result", retListPd);
		}
	}

	/**
	 * 获取类别信息
	 * 
	 * @param pd
	 * @throws Exception
	 */
	private void IC103(PageData pd) throws Exception {
		retPd=dao.getOne("ItemCategoryMapper.getCategory", pd);
		if (retPd != null) {
			json.put("status", "true");
			json.put("record", retPd);
		} else {
			json.put("status", "false");
			json.put("record", retPd);
		}
	}
	
	/**
	 * 保存类别信息
	 * 
	 * @param pd
	 * @throws Exception
	 */
	private void IC104(PageData pd) throws Exception {
		String action=pd.getString("action");
		Date d = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String dateNow = sdf.format(d);
		pd.put("CREATE_DATE", dateNow);
		HttpServletRequest request = pd.getRequest();
		HttpSession session = request.getSession();
		String CZRY = session.getAttribute("session_user").toString();
		pd.put("CZRY", CZRY);
		int row=0;
		if("add".equals(action)){
			row = dao.insert("ItemCategoryMapper.insertCategory", pd);
		}else
		{
			row = dao.update("ItemCategoryMapper.updateCategory", pd);
		}
		if(row>0){
			json.put("status", "true");
		}else{
			json.put("status", "false");
		}
		
	}
	
	/**
	 * 删除项目类别
	 * 
	 * @param pd
	 * @throws Exception
	 */
	private void IC105(PageData pd) throws Exception {
		String CODE = pd.getString("CODE");
		int rows = dao.delete("ItemCategoryMapper.delCategory", pd);
		int rows1 = dao.delete("ItemCategoryMapper.delItem", pd);
		if (rows > 0) {
			json.put("status", "true");
		} else {
			json.put("status", "false");
		}
	}
	/**
	 * 获取项目类别代码
	 * 
	 * @param pd
	 * @throws Exception
	 */
	private void IC106(PageData pd) throws Exception {
		String MAX_CODE="";
		retPd = dao.getOne("ItemCategoryMapper.getMaxCategoryCode", pd);
		if (retPd == null) {
			MAX_CODE = "1";
		} else {
			MAX_CODE = retPd.get("MAX_CODE").toString();
		}
		json.put("MAX_CODE", MAX_CODE);
	}

}
